package com.elf.gwt.common.client.subcomponent;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.common.client.CommonComponentController;
import com.elf.gwt.common.client.GWTCommerceCommonConstants;
import com.elf.gwt.common.client.access.JSONServerAccess;
import com.elf.gwt.common.client.access.JSONServerAccessCallback;
import com.elf.gwt.common.client.access.RequestParameter;
import com.elf.gwt.common.client.model.AbstractModel;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  Subcomponent for captcha
 */
public class CaptchaSubComponent extends CommonSubComponent
{
	protected JSONServerAccess jsonServerAccess;
	protected List<RequestParameter> requestParameters;
	protected String captchaSession;
	protected Image captchaImage;
	protected TextBox captchaCodeBox;

	/**
	 * Class that defines the callback behavior on the JSONServerAccess Response.
	 * 
	 */
	protected class CaptchaSubComponentAccessCallback implements JSONServerAccessCallback 
	{
		public void onError(List<RequestParameter> requestParameterList, Throwable exception)
		{
			buildFromModel();
		}

		public void onResponseReceived(List<RequestParameter> requestParameterList, JSONValue jsonValue)
		{
			build(jsonValue);
		}
	}
	/**
	 * Constructor
	 */
	public CaptchaSubComponent(
			CommonComponentController controller)
	{
		super("CaptchaComponent", null, null);
		
		jsonServerAccess = new JSONServerAccess();
		jsonServerAccess.setCallback(new CaptchaSubComponentAccessCallback());

		GWTCommerceCommonConstants constants = controller.getCommonConstants();
		
		thisComponent.add(new InlineHTML("<br/>"));
		CommonUtils.addSubTitle(handle, thisComponent, constants.CaptchaSecurity());
		Grid captchaGrid = new Grid(2,3);
		captchaGrid.setWidth("100%");
		captchaGrid.setStyleName(handle + "SubPanel");
		captchaGrid.getColumnFormatter().setStyleName(0, handle + "SubPanelCol0");
		thisComponent.add(captchaGrid);
		
		int gridrow = 0;
		captchaImage = new Image("dummy.gif");
		CommonUtils.addGridWidget(handle, gridrow, captchaImage, captchaGrid, "captcha", constants.CaptchaSecurityCode(), constants.CaptchaSecurityCodeText());
		gridrow++;
		
		captchaCodeBox = new TextBox();
		CommonUtils.addGridWidget(handle, gridrow, captchaCodeBox, captchaGrid, "captcha_code", constants.CaptchaVerifyCode(), constants.CaptchaVerifyCodeText());
		gridrow++;
		
		requestParameters = new ArrayList<RequestParameter>();
		requestParameters.add(new RequestParameter("handle", getHandle()));
		requestParameters.add(new RequestParameter("action", "CaptchaSetup"));
	}
	
	public void build()
	{
		captchaSession = null;
		jsonServerAccess.doServerAccess(requestParameters);
	}

	/**
	 * Build from jsonValue
	 * @param jsonValue
	 */
	public void build(JSONValue jsonValue)
	{
		try {
			JSONValue componentValue = CommonUtils.getComponentValue(jsonValue, getHandle());
			if (componentValue != null)
			{
				JSONArray jsonArray;
				if ((jsonArray = componentValue.isArray()) != null)
				{
					buildModel(jsonArray);
					buildFromModel();
				}
			}
		}
		catch (Exception e)
		{
			buildFromModel();
		}
	}

	public void buildFromModel()
	{
		if (captchaSession == null || captchaSession.length() == 0)
			captchaImage.setUrl("dummy.gif");
		else
		{
			String url = JSONServerAccess.DEFAULT_REQUEST_URL + "?handle=" + getHandle() + "&action=CaptchaImage&captcha_session=" + captchaSession;
			captchaImage.setUrl(url);
		}
	}

	/**
	 * BuildData from JSONArray
	 * @param jsonArray
	 */
	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		int sz = jsonArray.size();		
		for (int i = 0; i < sz; i++)
		{
			JSONValue jsonValue = jsonArray.get(i);
			JSONObject jsonObject;
			if ((jsonObject = jsonValue.isObject()) != null)
			{
				buildModel(jsonObject);
			}
		}
	}

	/**
	 * BuildData from JSONObject
	 * @param jsonObject
	 */
	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		captchaSession = AbstractModel.convertToString(jsonObject,"captcha_session");
		captchaCodeBox.setText("");
	}
	
	public String getCaptchaCode()
	{
		return captchaCodeBox.getText();
	}
	
	public String getCaptchaSession()
	{
		return captchaSession;
	}
}
